class Location{
	//打开地图选择位置
	static choose(){
		return new Promise((resolve,reject) => {
			uni.chooseLocation({
			    success: (res) => {
			        console.log('位置名称：' + res.name);
			        console.log('详细地址：' + res.address);
			        console.log('纬度：' + res.latitude);
			        console.log('经度：' + res.longitude);
					resolve(res);
			    },
				fail: (err) => {
					console.log(err)
					//授权被拒绝重新授权并重新执行方法
					this.onFailDetail(err,async () => {
						let location = await this.choose();
						resolve(location)
					})
				}
			});
		})
	}
	
	//当权限被拒绝的时候的处理
	static onFailDetail(err,callback){
		return new Promise((resolve,reject) => {
			//授权被拒绝
			if(err.errMsg == 'chooseLocation:fail auth deny'){
				uni.showModal({
					content: '请授权获取位置信息权限才可使用定位功能',
					success: async (e) => {
						if(e.confirm){
							await this.openSetting();
							callback()
						}
					}
				})
			}
		})
	}
	
	//打开授权页面获取权限
	static openSetting(){
		return new Promise((resolve,reject) => {
			uni.openSetting({
				success: (res) => {
					if(res.authSetting['scope.userLocation']) resolve(res);
					else reject(res)
				},
				fail: (err) => {
					reject(err)
				}
			})
		})
	}
	
	//传入经纬度打开获取在地图上的位置，不可选择定位
	static open({
		longitude,
		latitude,
		name,
		address
	}){
		uni.openLocation({
			longitude,
			latitude,
			name,
			address,
			success: function() {
				console.log('success');
			},
			fail: (err) => {
				console.log(err)
				uni.showToast({
					icon: 'none',
					title: '请先获取位置信息'
				})
			}
		});
	}
	
	//直接授权获取用户当前位置的经纬度
	static async getLocation(){
		return new Promise((resolve,reject) => {
			uni.getLocation({
				type: 'wgs84',
				success: res => {
					console.log(res)
					resolve(res);
				},
				fail: (err) => {
					console.log(err)
					//授权被用户拒绝了
					this.onFailDetail(err,async () => {
						let location = await this.getLocation();
						resolve(location)
					})
				}
			})
		})
	}
}
export default Location;